﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Kelper.Infrastructure;
using Kelper.Infrastructure.ModelBinders;
using Kelper.Domain.Models;

namespace Kelper
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "",
                "{action}",
                new { controller = "user" },
                new { action = "register|login|logout" }
            );
            routes.MapRoute(
                "",
                "{controller}/{name}/{page}",
                new { action = "view", page = 1 },
                new { controller = "category|ingredient" }
            );

            routes.MapRoute(
                "",
                "{controller}/{id}/{title}",
                new { controller = "recipe", action = "index", title = UrlParameter.Optional },
                new { id = @"\d+", controller = "recipe" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ModelBinders.Binders.Add(typeof(IList<Ingredient>), new IngredientsModelBinder());
            ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
        }
    }
}